package main

import (
	"fmt"
	"sync"
	"time"
)

var (
	ch4 = make(chan int)
	ch5 = make(chan int)
	ch6 = make(chan int)
	wg1 sync.WaitGroup
)

func main() {
	wg1.Add(1)

	go func() {
		defer wg1.Done()
		for i := 0; i < 10; i++ {
			ch4 <- i
			ch5 <- i
			ch6 <- i
		}
		close(ch4)
		close(ch5)
		close(ch6)
	}()

	// 设置一个每次循环都生效的超时机制
	for {
		select {
		case v, ok := <-ch4:
			if !ok {
				fmt.Println("Channel已关闭")
				return
			}
			fmt.Printf("主goroutine接收到 ch4: %d\n", v)
		case v2, ok := <-ch5:
			if !ok {
				fmt.Println("Channel ch5 已关闭")
				continue
			}
			fmt.Printf("主goroutine接收到 ch5: %d\n", v2)
		case v3, ok := <-ch6:
			if !ok {
				fmt.Println("Channel ch6 已关闭")
				continue
			}
			fmt.Printf("主goroutine接收到 ch6: %d\n", v3)
		case <-time.After(2 * time.Second):
			fmt.Println("操作超时")
			return
		}
	}
}
